package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     *
     * @param dishDTO 前端传来新增菜品信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addDish(DishDTO dishDTO) {
        //创建菜品对象
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //新增菜品基本信息
        dishMapper.addDishInfo(dish);
        //得到菜品的口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!CollectionUtils.isEmpty(flavors)) {
            //遍历拿到每种口味
            for (DishFlavor f : flavors) {
                //给每种口味设置菜品id
                f.setDishId(dish.getId());
            }
            dishFlavorMapper.addDishFlavor(flavors);
        }
    }

    /**
     * 菜品分页查询
     *
     * @param queryDTO 前端传来条件查询参数
     * @return
     */
    @Override
    public PageResult getDishPage(DishPageQueryDTO queryDTO) {
        //设置分页参数
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getPageSize());
        //查询菜品信息
        Page<DishVO> dishPage = dishMapper.getDishPage(queryDTO);
        return new PageResult(dishPage.getTotal(), dishPage.getResult());
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDish(List<Long> ids) {

        //根据ids查询，判断是否是起售中的菜品
        Integer count = dishMapper.getCountStatus(ids);
        if (count > 0) {
            log.info("起售中的菜品不能删除");
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        //根据ids查询，判断是否是被套餐关联的菜品
        Integer sum = setmealDishMapper.getCountById(ids);
        if (sum > 0) {
            log.info("被套餐关联的菜品不能删除");
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品和菜品对应的口味
            //删除菜品
            dishMapper.deleteDishById(ids);
            //删除菜品对应的口味
            dishFlavorMapper.deletedishFlavorByDishId(ids);


    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishVO queryDishById(Long id) {
        //根据ID查询菜品基本信息
        DishVO dishVO = dishMapper.queryDishInfoById(id);
        //根据菜品ID查询其口味
       List<DishFlavor> dishFlavorList = dishFlavorMapper.queryDishFlavorInfoByDishId(id);
       //判断口味是否为空
       if (!CollectionUtils.isEmpty(dishFlavorList)) {
           //将口味信息存入dishVO对象中
           dishVO.setFlavors(dishFlavorList);
       }
        return dishVO;
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDish(DishDTO dishDTO) {
        //创建菜品对象
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //修改菜品基本信息
        dishMapper.updateDishInfo(dish);
        //获取菜品口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //将原来的口味先删后添加
        dishFlavorMapper.deletedishFlavorByDishId(Arrays.asList(dish.getId()));
        if (!CollectionUtils.isEmpty(flavors)) {
            for (DishFlavor f : flavors) {
                f.setDishId(dish.getId());
            }
            //添加口味
            dishFlavorMapper.addDishFlavor(flavors);
        }
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> queryAllDishByCategoryId(Long categoryId) {
      List<Dish> dishList = dishMapper.queryAllDishByCategoryId(categoryId);
      return dishList;
    }

    /**
     * 菜品起售、停售
     *
     * @param status
     * @param id
     */
    @Override
    public void updateDishStatus(Integer status, Long id) {
        //创建菜品对象
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.updateDishInfo(dish);
    }

    /**
     * 条件查询菜品和口味
     *
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.queryAllDishByCategoryId(dish.getCategoryId());

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.queryDishFlavorInfoByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
